package com.mz.mattress.client.api.biz;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Order;
import com.mz.mattress.api.jwt.ClientJwt;
import com.mz.mattress.api.req.PrepayWxReq;
import com.mz.mattress.api.util.IpUtil;
import com.mz.mattress.client.api.req.SubmitOrderReq;
import com.mz.mattress.client.api.req.UseOrderReq;
import com.mz.mattress.client.api.resp.SubmitOrderResp;
import com.mz.mattress.client.common.Holder;
import com.mz.mattress.client.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/useListPage")
    public Result<IPage<Order>> useListPage(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        return orderService.useListPage(clientJwt.getMemberId(), pageNo, pageSize);
    }

    @GetMapping("/listPage")
    public Result<IPage<Order>> listPage(@RequestParam(required = false) Integer status,
                                         @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                         @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        return orderService.listPage(clientJwt.getMemberId(), status, pageNo, pageSize);
    }

    @GetMapping("/list")
    public Result<List<Order>> list() {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        return orderService.list(clientJwt.getMemberId());
    }

    @PostMapping(value = "submit")
    public Result<SubmitOrderResp> submit(@RequestBody SubmitOrderReq req) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        Long memberId = clientJwt.getMemberId();
        req.setMemberId(memberId);
        return orderService.submit(req);
    }

    @GetMapping("/{orderId}")
    public Result detail(@PathVariable String orderId) {
        return orderService.detail(Long.valueOf(orderId));
    }

    @PostMapping("prepay")
    public Result<Object> prepay(@RequestBody PrepayWxReq req, HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);
        req.setIp(ip);
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        req.setOpenId(clientJwt.getOpenId());
        return orderService.prepay(req);
    }

    @RequestMapping("queryIsPaid")
    public Result<Integer> queryIsPaid(@RequestParam Long orderId, @RequestParam(required = false) String outTradeNo) {
        return orderService.queryIsPaid(orderId, outTradeNo);
    }

    @GetMapping("/cancel/{orderId}")
    public Result cancel(@PathVariable String orderId) {
        return orderService.cancel(Long.valueOf(orderId));
    }

    @GetMapping("doRefundMoney")
    public Result<Boolean> doRefundMoney(@RequestParam Long orderId, @RequestParam Integer money) {
        return orderService.doRefundMoney(orderId, money);
    }

    @PostMapping(value = "bindDevice")
    public Result<Integer> bindDevice(@RequestBody UseOrderReq req) {
        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
        Long memberId = clientJwt.getMemberId();
        req.setMemberId(memberId);
        req.setDev(null);
        return orderService.bindDevice(req);
    }

}
